package algorithm;

/**
 * 83. 删除排序链表中的重复元素
 */
public class T00083 {
    public static void main(String[] args) {
        ListNode head = build(1,1,2,3,3);
        print(head);
        print(new Solution().deleteDuplicates(head));
    }

    static void print(ListNode head) {
        if(head == null)
            return;
        ListNode cur = head;
        for(;;) {
            System.out.print(cur.val);
            if((cur = cur.next) == null)
                break;
            System.out.print(" -> ");
        }
        System.out.println();
    }

    static ListNode build(int... arr) {
        ListNode n = null;
        for (int i = arr.length - 1; i >= 0; i --) {
            ListNode t = new ListNode(arr[i]);
            t.next = n;
            n = t;
        }
        return n;
    }

static class Solution {
    public ListNode deleteDuplicates(ListNode head) {
        if(head == null || head.next == null)
            return head;
        for(ListNode cur = head, next = cur.next; next != null; cur = next, next = next.next) {
            while(next != null && cur.val == next.val)
                next = next.next;
            cur.next = next;
            if(next == null)
                break;
        }
        return head;
    }
}
    private static class ListNode {
        int val;
        ListNode next;
        ListNode(int x) { val = x; }
    }
}